//
//  HDTabBarController.m
//  AccountEasy
//
//  Created by mac on 2020/7/22.
//  Copyright © 2020 AccountEasy. All rights reserved.
//

#import "HDTabBarController.h"
#import "HDTabBarItem.h"
#import "SAHomeVC.h"
#import "SAInventoryVC.h"
#import "SAStatisticsVC.h"
#import "SAMySelfVC.h"
#import "SAShopSaleVC.h"
#import "MainTabbar.h"

#define kTabBarTitle @[@"首页",@"库存",@"开单",@"统计",@"设置"]
//#define kTabBarTitle @[@"首页",@"库存",@"设置"]

@interface HDTabBarController ()<MainTabbarDelegate, UITabBarControllerDelegate>

@property (nonatomic, strong) NSMutableArray <HDTabBarItem *> *tabBarItems;
@property (nonatomic, strong) HDTabBarItem *lastSelectItem;
@property (nonatomic, strong) UILabel *msgLab;
@property (nonatomic, strong) MainTabbar *mainTabbar;
@property (nonatomic, strong) UIButton *saleBtn;

@end

@implementation HDTabBarController

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;//白色状态栏
}

#pragma mark - view controller life cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    SAHomeVC *homeVC = [SAHomeVC new];
    SAInventoryVC *inventoryVC = [SAInventoryVC new];
    SAStatisticsVC *statisticsVC = [SAStatisticsVC new];
    SAStatisticsVC *statisticsVC2 = [SAStatisticsVC new];
    SAMySelfVC *mySelfVC = [SAMySelfVC new];
    
    self.viewControllers = @[homeVC,inventoryVC,statisticsVC2,statisticsVC,mySelfVC];
    //    self.viewControllers = @[homeVC,inventoryVC,mySelfVC];
    
    [self.tabBar removeFromSuperview];
    MainTabbar *mainTabbar = [[MainTabbar alloc] init];
    [mainTabbar.centerBtn addTarget:self action:@selector(clickedSaleBtn) forControlEvents:UIControlEventTouchUpInside];
    //选中时的颜色
    mainTabbar.tintColor = [UIColor colorWithRed:27.0/255.0 green:118.0/255.0 blue:208/255.0 alpha:1];
    //透明设置为NO，显示白色，view的高度到tabbar顶部截止，YES的话到底部
    mainTabbar.translucent = NO;
    //利用KVC 将自己的tabbar赋给系统tabBar
    [self setValue:mainTabbar forKeyPath:@"tabBar"];
    self.delegate = self;
    [self initTabBar];
    
    [UITabBar appearance].layer.borderWidth = 0.0f;
    [UITabBar appearance].clipsToBounds = YES;
}

#pragma mark - init view or data

- (void)initTabBar{
    
    _tabBarItems = [NSMutableArray array];
    
    CGFloat itemWidth, itemHieght;
    
    itemWidth = kScreenWidth / self.viewControllers.count;
    itemHieght = 49;
    
    UIView *customBar = [[UIView alloc] init];
    [self.view addSubview:customBar];
    
    if (KIsiPhoneX) {
        itemHieght = 49 + SafeFooterHeight/kScreenScale;
    }
    customBar.backgroundColor = kColor(255, 255, 255, 1);;
    
    [customBar mas_makeConstraints:^(MASConstraintMaker *make) {
        kMasLeft(0);kMasBottom(0);kMasWidth(kBaseScreenWidth);kMasHeight(itemHieght);
    }];
    
    for (int i = 0; i < self.viewControllers.count; i++) {
        HDTabBarItem *item = [HDTabBarItem tabBarItemWithTitle:kTabBarTitle[i] normalImg:[NSString stringWithFormat:@"tabbar_normal_%d",i+1] selectImg:[NSString stringWithFormat:@"tabbar_select_%d",i+1]];
        [item addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
        item.frame = CGRectMake(itemWidth * i, 0, itemWidth, kScaleNum(49));
        [_tabBarItems addObject:item];
        [customBar addSubview:item];
        if (i == 2) {
            item.userInteractionEnabled = NO;
        }else {
            item.userInteractionEnabled = YES;
        }
    }
    UIButton *saleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saleBtn.frame = CGRectMake(itemWidth * 2, kScaleNum(-25), itemWidth, kScaleNum(74));
    saleBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    [saleBtn setImage:[UIImage imageNamed:@"tabbar_add"] forState:UIControlStateNormal];
    [saleBtn addTarget:self action:@selector(clickedSaleBtn) forControlEvents:UIControlEventTouchUpInside];
    [customBar addSubview:saleBtn];
    self.saleBtn = saleBtn;
    
    [self selectedItem:_tabBarItems[0]];
}

#pragma mark - view events

- (void)selectedItem:(HDTabBarItem *)item {
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    if ([item.titleLabel.text isEqualToString:@"库存"]) {
        NSString *inventory = [NSString stringWithFormat:@"%@",menuList[3][@"isDelete"]];
        NSString *stockMerchandiseInventory = [NSString stringWithFormat:@"%@",menuList[3][@"perms"][@"stockMerchandiseInventory"]];
        if ([inventory isEqualToString:@"1"]) {
            [Prompt popPromptViewWithMsg:@"您没有库存权限,请联系店长添加" duration:2];
            return;
        }else if ([stockMerchandiseInventory isEqualToString:@"1"]) {
            [Prompt popPromptViewWithMsg:@"您没有商品库存权限,请联系店长添加" duration:2];
            return;
        }
    }else if ([item.titleLabel.text isEqualToString:@"设置"]) {
        NSString *isSet = [NSString stringWithFormat:@"%@",menuList[9][@"isDelete"]];
        NSString *systemSettingGlobalView = [NSString stringWithFormat:@"%@",menuList[9][@"perms"][@"systemSettingGlobalView"]];
        if ([isSet isEqualToString:@"1"]) {
            [Prompt popPromptViewWithMsg:@"您没有设置权限,请联系店长添加" duration:2];
            return;
        }else if ([systemSettingGlobalView isEqualToString:@"1"]) {
            [Prompt popPromptViewWithMsg:@"您没有全局设置权限,请联系店长添加" duration:2];
            return;
        }
    }else if ([item.titleLabel.text isEqualToString:@"统计"]) {
        NSString *isStatistical = [NSString stringWithFormat:@"%@",menuList[8][@"isDelete"]];
        if ([isStatistical isEqualToString:@"1"]) {
            [Prompt popPromptViewWithMsg:@"您没有统计权限,请联系店长添加" duration:2];
            return;
        }
    }
    
    _lastSelectItem.selected = NO;
    _lastSelectItem.backgroundColor = kColor(255, 255, 255, 1);
    [item setBackgroundColor:kColor(255, 255, 255, 1)];
    item.selected = YES;
    self.selectedIndex = [self.tabBarItems indexOfObject:item];
    _lastSelectItem = item;
}

- (void)setSelectIndex:(NSInteger)index
{
    
    _lastSelectItem.selected = NO;
    _lastSelectItem.backgroundColor = kColor(255, 255, 255, 1);
    
    HDTabBarItem *item = self.tabBarItems[index];
    [item setBackgroundColor:kColor(255, 255, 255, 1)];
    item.selected = YES;
    
    self.selectedIndex = [self.tabBarItems indexOfObject:item];
    
    _lastSelectItem = item;
}

-(void)clickedSaleBtn {
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *isShopSale = [NSString stringWithFormat:@"%@",menuList[0][@"isDelete"]];
    if ([isShopSale isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有销售开单权限,请联系店长添加" duration:2];
        return;;
    }
    SAShopSaleVC *pushVC = [SAShopSaleVC new];
    [self.navigationController pushViewController:pushVC animated:YES];
}


- (void)addChildVC:(UIViewController *)vc imageName:(NSString *)imageName selectedImage:(NSString *)selectedImage {
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

#pragma mark - UITabBarControllerDelegate 代理
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    self.mainTabbar.centerBtn.selected = (tabBarController.selectedIndex == 2);
}

#pragma mark - MainTabbarDelegate 代理
- (void)clickCenterBtnActionMainTabbar:(MainTabbar *)mainTabbar {
    self.selectedIndex = 2;
}

-(void)dealloc
{
    HDLog(@"%@ 控制器已经被释放",NSStringFromClass([self class]));
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
